package com.hangzhou.welbeing.welbeinginstrument.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.base.MyApplication;
import com.hangzhou.welbeing.welbeinginstrument.bean.CommentsBean;
import com.hangzhou.welbeing.welbeinginstrument.bean.GuessYouLikeDateBean;
import com.hangzhou.welbeing.welbeinginstrument.bean.PraiseBean;
import com.hangzhou.welbeing.welbeinginstrument.config.RBConstants;
import com.hangzhou.welbeing.welbeinginstrument.utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 时间 2017 年 09 月 29.
 * 作者 Administrator  刘传华
 * 公司 杭州惟君医疗科技有限公司
 * 包名 com.hangzhou.welbeing.welbeinginstrument.activity
 * 工程名 welbeing_pregnant
 */
public class CommentsActivity extends Activity {
    @Bind(R.id.activity_editing_tv_cancle)
    TextView activityEditingTvCancle;
    @Bind(R.id.activity_editing_tv_posted)
    TextView activityEditingTvPosted;
    @Bind(R.id.activity_comments_et_)
    EditText activityCommentsEt;
    private GuessYouLikeDateBean.DataBean datalist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comments);
        ButterKnife.bind(this);
        Utils.setStatusBar(this, false, false);
        Utils.setStatusTextColor(true, this);
        datalist = (GuessYouLikeDateBean.DataBean) getIntent().getSerializableExtra("datalist");

    }

    @OnClick({R.id.activity_editing_tv_cancle, R.id.activity_editing_tv_posted})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_editing_tv_cancle:
                overridePendingTransition(0, R.anim.activity_anim_scenic_out);
                finish();
                break;
            case R.id.activity_editing_tv_posted:
               String  Comments = activityCommentsEt.getText().toString().trim();
                if(!TextUtils.isEmpty(Comments)){
//                    if(comments_02.equals("0")){
//                        submitComments(Comments);
//                    }else if(comments_02.equals("1")){
//                        submitRevert(parent,Comments,po_id);
//                    }

                }else{
                    Toast.makeText(CommentsActivity.this,"请输入你的评论或回复",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    /**
     * @desc 评论  {"errorCode":200,"errorMessage":"ok"}
     * @param etCommentariesContent*/
    private void submitComments(String etCommentariesContent) {
        SharedPreferences sp = getSharedPreferences("second_app_internal_data", 0);
        String  user_id = sp.getString("user_id","");
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL+RBConstants.COMMENTS)
                .addParams("cus_id", user_id)
                .addParams("po_id", "")
                .addParams("content", etCommentariesContent)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String praiseParseNetworkResponse =  response.body().string();
                        Gson gson = new Gson();
                        // Log.e(TAG,"praiseParseNetworkResponse============>"+praiseParseNetworkResponse);
                        final CommentsBean commentsBean = gson.fromJson(praiseParseNetworkResponse,CommentsBean.class);
                        if(commentsBean.errorCode == 200 ){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(CommentsActivity.this,"评论成功",Toast.LENGTH_SHORT).show();
//                                    Intent intent = new Intent(CommentsActivity.this,SimilarProfilePageFocusedActivity.class);
//                                    intent.putExtra("po_id",po_id);
//                                    intent.putExtra("comments_02","0");
//                                    intent.putExtra("parent","");
//
//                                    intent.putExtra("po_url",po_url);
//                                    intent.putExtra("po_name",po_name);
//                                    intent.putExtra("content",content);
//                                    intent.putExtra("cus_logo",cus_logo);
//                                    intent.putExtra("cus_name",cus_name);
//                                    intent.putExtra("state",state);
//                                    intent.putExtra("lCount",lCount);
//                                    intent.putExtra("lstate",lstate);
//                                    intent.putExtra("count",count);
//                                    intent.putExtra("col",col);
//                                    intent.putExtra("hits_like",hits_like);
//                                    intent.putExtra("cus_id",cus_id);
//                                    startActivity(intent);
//                                    overridePendingTransition(0, R.anim.activity_anim_scenic_out);
                                  //  finish();
                                }
                            });
                        }else{
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(CommentsActivity.this,commentsBean.errorMessage,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }
    /**
     * @desc 回复
     * */
    private void submitRevert(String id1, String etCommentariesContent, String po_id_01) {
        SharedPreferences sp = getSharedPreferences("second_app_internal_data", 0);
        String  user_id = sp.getString("user_id","");
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL+RBConstants.COMMENTS)
                .addParams("parent",id1)
                .addParams("po_id", po_id_01)
                .addParams("content", etCommentariesContent)
                .addParams("cus_id", user_id)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String praiseParseNetworkResponse =  response.body().string();
                        Gson gson = new Gson();
                        // Log.e(TAG,"praiseParseNetworkResponse============>"+praiseParseNetworkResponse);
                        final PraiseBean praiseBean = gson.fromJson(praiseParseNetworkResponse,PraiseBean.class);
                        if(praiseBean.errorCode == 200){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(CommentsActivity.this,"回复成功",Toast.LENGTH_SHORT).show();
//                                    if(fen_bian_id.equals("0")){
//                                        Intent intent = new Intent(CommentsActivity.this,SimilarProfilePageFocusedActivity.class);
//                                        intent.putExtra("po_id",po_id);
//                                        intent.putExtra("comments_02","0");
//                                        intent.putExtra("parent","");
//
//                                        intent.putExtra("po_url",po_url);
//                                        intent.putExtra("po_name",po_name);
//                                        intent.putExtra("content",content);
//                                        intent.putExtra("cus_logo",cus_logo);
//                                        intent.putExtra("cus_name",cus_name);
//                                        intent.putExtra("state",state);
//                                        intent.putExtra("lCount",lCount);
//                                        intent.putExtra("lstate",lstate);
//                                        intent.putExtra("count",count);
//                                        intent.putExtra("col",col);
//                                        intent.putExtra("hits_like",hits_like);
//                                        intent.putExtra("cus_id",cus_id);
//                                        startActivity(intent);
//                                        finish();
//                                    }else if(fen_bian_id.equals("1")){
//                                        Intent intent = new Intent(CommentsActivity.this,AllCommentsActivity.class);
//                                        intent.putExtra("po_id",po_id);
//                                        intent.putExtra("comments_02","0");
//                                        intent.putExtra("parent","");
//
//                                        intent.putExtra("po_url",po_url);
//                                        intent.putExtra("po_name",po_name);
//                                        intent.putExtra("content",content);
//                                        intent.putExtra("cus_logo",cus_logo);
//                                        intent.putExtra("cus_name",cus_name);
//                                        intent.putExtra("state",state);
//                                        intent.putExtra("lCount",lCount);
//                                        intent.putExtra("lstate",lstate);
//                                        intent.putExtra("count",count);
//                                        intent.putExtra("col",col);
//                                        intent.putExtra("hits_like",hits_like);
//                                        intent.putExtra("cus_id",cus_id);
//                                        startActivity(intent);
//                                        finish();
//                                    }

                                }
                            });
                        }else{
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(CommentsActivity.this,praiseBean.errorMessage,Toast.LENGTH_SHORT).show();

                                }
                            });
                        }

                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }
}
